package com.pig4cloud.mall.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.mall.product.entity.ProductAttributeCategory;
import com.pig4cloud.mall.product.mapper.ProductAttributeCategoryMapper;
import com.pig4cloud.mall.product.service.ProductAttributeCategoryService;
import com.pig4cloud.pig.common.core.util.PageResult;
import com.pig4cloud.pig.common.core.util.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 产品属性分类表
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@Service
public class ProductAttributeCategoryServiceImpl extends ServiceImpl<ProductAttributeCategoryMapper, ProductAttributeCategory> implements ProductAttributeCategoryService {

	@Resource
	private ProductAttributeCategoryMapper attrCategoryMapper;

	@Override
	public PageResult listAttrCategory(Integer current, Integer size) {
		IPage<ProductAttributeCategory> iPage = this.page(new Page<>(current, size));
		return PageResult.buildPage(iPage);
	}

	@Override
	public int create(String name) {
		ProductAttributeCategory attrCategory = new ProductAttributeCategory();
		attrCategory.setName(name);
		return baseMapper.insert(attrCategory);
	}

	@Override
	public R delete(Long id) {
		ProductAttributeCategory attrCategory = baseMapper.selectById(id);
		if (attrCategory.getAttributeCount() > 0) {
			return R.failed("该商品类型下包含销售规格属性，无法删除！请删除关联属性后再操作！");
		}
		if (attrCategory.getParamCount()> 0) {
			return R.failed("该商品类型下包含基本参数属性，无法删除！请删除关联属性后再操作！");
		}
		int count = baseMapper.deleteById(id);
		if (count > 0) {
			return R.ok();
		} else {
			return R.failed("删除失败！");
		}
	}
}
